<?php

namespace App\Providers;

use App\Domain\Customer\RpcService\CustomerInterface;
use App\Domain\Customer\RpcService\CustomerService;
use App\Domain\FamilyMember\RpcService\FamilyMemberInterface;
use App\Domain\FamilyMember\RpcService\FamilyMemberService;
use App\Domain\HealthCheck\RpcServices\HealthCheckService;
use App\Domain\HealthCheck\RpcServices\HealthCheckServiceInterface;
use App\Domain\HealthHistory\RpcService\HealthHistoryInterface;
use App\Domain\HealthHistory\RpcService\HealthHistoryService;
use App\Domain\LifeHealth\RpcService\LifeHealthInterface;
use App\Domain\LifeHealth\RpcService\LifeHealthService;
use App\Domain\Visitlist\RpcService\VisitlistInterface;
use App\Domain\Visitlist\RpcService\VisitlistService;
use App\Supports\Util\PrintMessage;
use Godruoyi\Snowflake\LaravelSequenceResolver;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    public $singletons = [

    ];

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('snowflake', function () {
            return (new Snowflake())
                ->setStartTimeStamp(strtotime('2021-09-28') * 1000)
                ->setSequenceResolver(
                    new LaravelSequenceResolver($this->app->get('cache')->store()
                    ));
        });
        $this->app->singleton(FamilyMemberInterface::class,FamilyMemberService::class);
        $this->app->singleton(HealthCheckServiceInterface::class,HealthCheckService::class);
        $this->app->singleton(CustomerInterface::class,CustomerService::class);
        $this->app->singleton(VisitlistInterface::class,VisitlistService::class);
        $this->app->singleton(LifeHealthInterface::class,LifeHealthService::class);
        $this->app->singleton(HealthHistoryInterface::class,HealthHistoryService::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        $this->loadFunction();
        $this->app->singleton('PrintMessage', PrintMessage::class);
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            return $validator->validateRegex(
                $attribute,
                $value,
                ['/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/']
            );
        });
    }

    protected function loadFunction()
    {
        foreach (glob(app_path('Supports/Function') . '/*.php') as $file) {
            require_once $file;
        }
    }
}
